package com.spring.example.mybatis.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author zhouhengzhe
 * @description:
 * @date 2021/11/30 下午10:39
 * @since v1
 */
@Slf4j
public class ZhzInvocationHandler implements InvocationHandler {
	Class<?> target;
	public ZhzInvocationHandler(Class<?> target) {
		this.target=target;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

		System.out.println("假装连上数据库 JDBC");

		System.out.println("得到sql语句！！");
		if (method.isAnnotationPresent(Select.class)){
			Select select = method.getAnnotation(Select.class);
			String sql = select.value()[0];
			System.out.println("query："+sql);
		}
		if (method.getName().equals("toString")){
			String name = target.getName();
			return name;
		}
		return null;
	}
}
